package com.youdianstar.app.common.util

import android.content.Context
import android.widget.Toast

/**
 * File: ToastUtil
 * Author: 82149 Create: 2021/9/22 15:41
 * Changes (from 2021/9/22)
 * --------------------------------------------------
 * description: 考虑到多语言切换后要更新Context,不一定用AppUtil
 * ---------------------------------------------------
 *
 */
object ToastUtil {

    private var time: Long = 0
    private var oldMsg: String? = null

    fun show(context: Context?, msg: String) {
        if (msg != oldMsg) {
            create(context, msg)
            time = System.currentTimeMillis()
        } else {
            if (System.currentTimeMillis() - time > 2000) {
                create(context, msg)
                time = System.currentTimeMillis()
            }
        }
        oldMsg = msg
    }

    private fun create(context: Context?, massage: String) {
        val mContext = context ?: AppUtil.getApplication()?.applicationContext
        val toast = Toast.makeText(mContext, massage, Toast.LENGTH_SHORT)
        toast.show()
    }
}